mod common;

use common::{dourl, eq_obj_notime};
use serde_json::json;
use std::{file, line};

#[tokio::test]
async fn a_chunk() {
    let data = json!({
        "n": "chunk"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "v": {
                    "d1":11,
                    "d2":21,
                    "d3":21,
                }
            },
            {
                "psn": "p1001",
                "wsn": "w002",
                "v": {
                    "d1":12,
                    "d2":22,
                    "d3":22,
                }
            },
            {
                "psn": "p1001",
                "wsn": "w003",
                "v": {
                    "d1":12,
                    "d2":22,
                    "d3":22,
                }
            },
            {
                "psn": "p1002",
                "wsn": "w001",
                "v": {
                    "d1":13,
                    "d2":23,
                    "d3":23,
                }
            },
            {
                "psn": "p1002",
                "wsn": "w002",
                "v": {
                    "d1":14,
                    "d2":24,
                    "d3":24,
                }
            },
            {
                "psn": "p1002",
                "wsn": "w003",
                "v": {
                    "d1":14,
                    "d2":24,
                    "d3":24,
                }
            },
        ]
    });
    let ret = dourl(&data, "chunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk",
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "line": "line-one", "psn": "p1001", "wsn": "w001", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w002", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w003", "dtp": 0},
                    {"d1": 13, "d2": 23, "d3": 23, "line": "line-one", "psn": "p1002", "wsn": "w001", "dtp": 0},
                    {"d1": 14, "d2": 24, "d3": 24, "line": "line-one", "psn": "p1002", "wsn": "w002", "dtp": 0},
                    {"d1": 14, "d2": 24, "d3": 24, "line": "line-one", "psn": "p1002", "wsn": "w003", "dtp": 0},
                ]},
            }
        }),
        line!(),
        file!(),
    );

    let data = json!({
        "n": " chunk",
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "line": "line-one", "psn": "p1001", "wsn": "w001", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w002", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w003", "dtp": 0},
                    {"d1": 13, "d2": 23, "d3": 23, "line": "line-one", "psn": "p1002", "wsn": "w001", "dtp": 0},
                    {"d1": 14, "d2": 24, "d3": 24, "line": "line-one", "psn": "p1002", "wsn": "w002", "dtp": 0},
                    {"d1": 14, "d2": 24, "d3": 24, "line": "line-one", "psn": "p1002", "wsn": "w003", "dtp": 0},
                ]},
            }
        }),
        line!(),
        file!(),
    );

    let data = json!({
        "n": "chunk",
        "opt": {
            "and": [
                {"eq": "psn", "v": "p1001"}
            ]
        },
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "line": "line-one", "psn": "p1001", "wsn": "w001", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w002", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w003", "dtp": 0},
                ]},
            }
        }),
        line!(),
        file!(),
    );

    let data = json!({
        "n": " chunk",
        "opt": {
            "and": [
                {"eq": " psn", "v": "p1001"}
            ]
        },
        "obj": " *",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "line": "line-one", "psn": "p1001", "wsn": "w001", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w002", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w003", "dtp": 0},
                ]},
            }
        }),
        line!(),
        file!(),
    );

    let data = json!({
        "n": "chunk",
        "opt": {
            "and": [
                {"eq": "psn", "v": "p1001"},
                {"eq": "wsn", "v": "w001"}
            ]
        },
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "line": "line-one", "psn": "p1001", "wsn": "w001", "dtp": 0},
                ]},
            }
        }),
        line!(),
        file!(),
    );

    let data = json!({
        "n": "chunk",
        "opt": {
            "or": [
                {"eq": "wsn", "v": "w001"},
                {"eq": "wsn", "v": "w003"}
            ]
        },
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "line": "line-one", "psn": "p1001", "wsn": "w001", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w003", "dtp": 0},
                    {"d1": 13, "d2": 23, "d3": 23, "line": "line-one", "psn": "p1002", "wsn": "w001", "dtp": 0},
                    {"d1": 14, "d2": 24, "d3": 24, "line": "line-one", "psn": "p1002", "wsn": "w003", "dtp": 0},
                ]},
            }
        }),
        line!(),
        file!(),
    );

    let data = json!({
        "n": "chunk",
        "opt": {
            "or": [
                {"eq": "wsn", "v": "w001"},
                {"eq": "wsn", "v": "w003"}
            ]
        },
        "filter": {
            "and": [
                {"eq": "psn", "v": "p1001"},
            ]
        },
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "line": "line-one", "psn": "p1001", "wsn": "w001", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w003", "dtp": 0},
                ]},
            }
        }),
        line!(),
        file!(),
    );

    let data = json!({
        "n": "chunk",
        "opt": {
            "and": [
                {"eq": "psn", "v": "p1001"}
            ]
        },
        "filter": {
            "or": [
                {"eq": "wsn", "v": "w001"},
                {"eq": "wsn", "v": "w003"}
            ]
        },
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "line": "line-one", "psn": "p1001", "wsn": "w001", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "line": "line-one", "psn": "p1001", "wsn": "w003", "dtp": 0},
                ]},
            }
        }),
        line!(),
        file!(),
    );
}

#[tokio::test]
async fn b_chunk() {
    let data = json!({
        "n": "chunk",
        "opt": {
            "and": {"eq": "psn", "v": "p1001"}
        },
        "obj": "*",
    });
    let ret = dourl(&data, "get").await.expect_err("error_4");
    assert_eq!(
        ret,
        json!({
            "code": 2001,
            "fail": true,
            "success": false,
            "message": r#"miss error: "and""#,
        })
    );

    let data = json!({
        "n": "chunk",
        "opt": {
            "or": {"eq": "psn", "v": "p1001"}
        },
        "obj": "*",
    });
    let ret = dourl(&data, "get").await.expect_err("error_4");
    assert_eq!(
        ret,
        json!({
            "code": 2001,
            "fail": true,
            "success": false,
            "message": r#"miss error: "or""#,
        })
    );

    let data = json!({
        "n": "chunk",
        "opt": {
            "or": [{"eq": "psn", "v": "p1001"}]
        },
        "filter": {
            "and": {"eq": "wsn", "v": "w001"},
        },
        "obj": "*",
    });
    let ret = dourl(&data, "get").await.expect_err("error_4");
    assert_eq!(
        ret,
        json!({
            "code": 2001,
            "fail": true,
            "success": false,
            "message": r#"miss error: "and""#,
        })
    );

    let data = json!({
        "n": "chunk",
        "opt": {
            "or": [{"eq": "psn", "v": "p1001"}]
        },
        "filter": {
            "or": {"eq": "wsn", "v": "w001"},
        },
        "obj": "*",
    });
    let ret = dourl(&data, "get").await.expect_err("error_4");
    assert_eq!(
        ret,
        json!({
            "code": 2001,
            "fail": true,
            "success": false,
            "message": r#"miss error: "or""#,
        })
    );
}
